package jp.co.iris_ltd.irisphotoeditor;

import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;

import android.content.Context;
import android.content.res.TypedArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import android.graphics.drawable.Drawable;

public class ResouseListAdapter extends BaseAdapter {
    private int mResourceId;
    private TypedArray mItems;
    private LayoutInflater mInflater;

    public ResouseListAdapter(Context context, int resource, TypedArray items) {
        mInflater = LayoutInflater.from(context);
        mResourceId = resource;
        mItems = items;
    }

    @Override
    public int getCount() {
        return mItems.length();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = mInflater.inflate(mResourceId, null);
            convertView.setLayoutParams(new GridView.LayoutParams(100, 100));
        }

        ImageView iconImage = (ImageView) convertView.findViewById(R.id.image);
        TextView iconName = (TextView) convertView.findViewById(R.id.name);

        if (mItems.length() > position) {
            Drawable drawable = mItems.getDrawable(position);
            iconImage.setImageDrawable(drawable);
            iconName.setVisibility(View.GONE);
        } else {
            iconImage.setVisibility(View.GONE);
            iconName.setText("画像が存在しません");
        }

        return convertView;
    }

}
